home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / program / freeli22.zip / PROGS / ZD.ASM < prev    next >
Assembly Source File  |  1996-09-01  |  4KB  |  156 lines

  1. ; ZD: Decodes files produced with ZE
  2.  
  3. Ideal
  4. Jumps
  5.  
  6. Public      main
  7. Extrn       startup:near
  8.  
  9. Macro       lcall p,a,b,c,d,e,f,g,h ;; library call
  10.  
  11.             ifnb <a>
  12.               push a                ;; if args, push first arg
  13.               lcall p,b,c,d,e,f,g,h ;; and recurse . . .
  14.             else
  15.               extrn p:near          ;; declare procedure
  16.               call p                ;; call procedure
  17.             endif
  18.  
  19. EndM
  20.  
  21. Model Tiny
  22. Codeseg
  23. P186
  24. Org 100h
  25.  
  26. Start:      jmp startup
  27.  
  28. ;****************** Data Section
  29.  
  30. FSize       dw 0,0                  ;File size
  31.  
  32. ;****************** Strings Section
  33.  
  34. Syntax      db 'Syntax:  ZD <infile> <outfile>',0
  35.  
  36. ;****************** 'main' procedure
  37.  
  38. Proc        main
  39.  
  40.             lcall fsetbuf 16384     ;Set file buffers to 16K
  41.  
  42.             cmp cx,2                ;Wrong number of args?
  43.             jne m_syntax
  44.  
  45.             lcall fopen [di],0      ;Open input file
  46.             test ax,ax              ;File not found?
  47.             jz m_syntax
  48.             xchg bp,ax              ;BP = handle
  49.  
  50.             lcall fopen [di+2],3    ;Open output file
  51.             test ax,ax              ;Check for errors
  52.             jnz m_ok1
  53.             lcall fclose bp         ;Close input file
  54.             jmp m_syntax            ;Go print syntax
  55.  
  56. m_ok1:      xchg di,ax              ;DI = handle
  57.  
  58.             mov si,8                ;8 digits
  59.  
  60. m_sloop:    mov dx,4                ;4 bits
  61.  
  62. m_01:       add bx,bx               ;Shift in bit
  63.             adc cx,cx
  64.             dec dx                  ;Loop back
  65.             jnz m_01
  66.  
  67. m_02:       lcall fgetc bp          ;Get hex digit
  68.             cmp al,'0'
  69.             jb m_02
  70.             cmp al,'F'
  71.             ja m_02
  72.  
  73.             cmp al,3Ah              ;Convert to binary
  74.             jb $+4
  75.             sub al,7
  76.             and al,0Fh
  77.             or bl,al                ;OR in digit
  78.             dec si
  79.             jnz m_sloop             ;Loop back
  80.  
  81.             mov [FSize],bx          ;Save file size
  82.             mov [FSize+2],cx
  83.  
  84.             mov dl,91               ;DL = multiplier
  85.  
  86. m_loop:     lcall fgetc bp          ;Load char
  87.             cmp al,7Eh              ;Check bounds
  88.             je m_done               ;Tilde '~' = end
  89.             ja m_loop
  90.             cmp al,21h
  91.             jb m_loop
  92.  
  93.             sub al,21h              ;Remove offset
  94.             mul dl                  ;BX = AL * 91
  95.             xchg bx,ax
  96.  
  97.             lcall fgetc bp          ;Get char
  98.             sub al,21h              ;Remove offset
  99.             xor ah,ah               ;Add in remainder
  100.             add bx,ax
  101.  
  102.             push di bx              ;Output 13 bits
  103.             call Put13Bits
  104.             jmp m_loop              ;Loop back
  105.  
  106. m_done:     lcall fclose bp         ;Close files
  107.             lcall fclose di
  108.             ret                     ;Return
  109.  
  110. m_syntax:   push offset(Syntax)     ;Display 'Syntax' message
  111.             lcall puts
  112.             ret                     ;Return
  113.  
  114. EndP        main
  115.  
  116. ;****************** Put13Bits -- Put 13 bits to output file
  117. ;void Put13Bits(FILE *input, int code);
  118.  
  119. Proc        Put13Bits
  120.  
  121.             push bp                 ;Set up stack frame
  122.             mov bp,sp
  123.             pusha                   ;Save registers
  124.  
  125.             mov dx,[bp+6]           ;DX = file
  126.             mov bx,[bp+4]           ;BX = shifted code
  127.             shl bx,3
  128.             mov cx,13               ;13 bits
  129.  
  130. pb_loop:    add bx,bx               ;Shift out bit
  131.             jnc pb_skip1            ;Check bit
  132.             mov al,[BitMask]        ;Add bit to buffer
  133.             or [BitBuf],al
  134.  
  135. pb_skip1:   ror [BitMask],1         ;Rotate mask
  136.             jnc pb_skip2            ;Check for wrap
  137.             mov al,[BitBuf]         ;AL = byte
  138.             mov [BitBuf],0          ;Clear bit buffer
  139.             sub [FSize],1           ;Decrement size
  140.             sbb [FSize+2],0         ;Enough data?
  141.             jl pb_skip2
  142.             lcall fputc dx,ax       ;Output byte
  143.  
  144. pb_skip2:   loop pb_loop            ;Loop back
  145.  
  146.             popa                    ;Restore registers
  147.             pop bp                  ;Delete stack frame
  148.             ret 4                   ;Return
  149.  
  150. BitMask     db 80h                  ;Mask and buffer
  151. BitBuf      db 0
  152.  
  153. EndP        Put13Bits
  154.  
  155. End Start
  156.